/*
 * RSSTreeHelper.java
 *
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.afaker.rss.gui.tree;

import com.afaker.rss.context.ApplicationContext;
import com.afaker.rss.gui.action.MenuAction;
import com.afaker.rss.xml.XMLDocumentFactory;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
import org.jdom.Element;

/**
 *
 * @author new
 */
public class RSSTreeHelper {
    private JTree tree;
    private RSSTreeNode root ;
    private ArrayList<RSSTreeNode> feedNodes = new ArrayList<RSSTreeNode>();
    /**
     * Creates a new instance of RSSTreeHelper
     */
    public RSSTreeHelper(JTree tree) {
        this.tree=tree;
        root = (RSSTreeNode)tree.getModel().getRoot();
    }
    /**
     * Expanding or Collapsing All Nodes in a JTree Component
     * If expand is true, expands all nodes in the tree.
     * Otherwise, collapses all nodes in the tree.
     */
    public void expandAll(boolean expand) {
        
        // Traverse tree from root
        expandAll(new TreePath(root), expand);
    }
    private void expandAll(TreePath parent, boolean expand) {
        // Traverse children
        RSSTreeNode node = (RSSTreeNode)parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e=node.children(); e.hasMoreElements(); ) {
                RSSTreeNode n = (RSSTreeNode)e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(path, expand);
            }
        }
        
        // Expansion or collapse must be done bottom-up
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }
    
    public  void setTreeWithXML(XMLDocumentFactory xml) {
        Element favorite=xml.getRootElement().getChild(MenuAction.FAVORITE_NODE_NAME);
        favorite.setAttribute(MenuAction.FAVORITE_N_CATEGORY_NAME_ATTRIBUTE_NAME,
                ApplicationContext.getApplicationContext().getProperty("category.root.text"));
        root.setUserObject(favorite);
        recursiveAddTreeNode(favorite,root,MenuAction.CATEGORY_NODE_NAME);
    }
    
    private  void recursiveAddTreeNode(Element parent,RSSTreeNode subroot,String target) {
        Iterator it=parent.getChildren(target).listIterator();
        RSSTreeNode temp;
        Element next;
        String title = null;
        while(it.hasNext()) {
            next=(Element) it.next();
            if(target.equals(MenuAction.CATEGORY_NODE_NAME)) {
                temp=new RSSTreeNode(next);
                temp.setAllowsChildren(true);
            } else {
                temp=new RSSTreeNode(next);
                temp.setAllowsChildren(false);
                feedNodes.add(temp);
            }
            subroot.add(temp);
            recursiveAddTreeNode(next,temp,MenuAction.CHANNEL_NODE_NAME);
            recursiveAddTreeNode(next,temp,target);
        }
    }
    
    public ArrayList<RSSTreeNode> getFeedTreeNode() {
        return feedNodes;
    }
}
